<?php

class SiteLayout
{
	static function imagePath($src)
	{
		$imagesPath = '/public/css/images/'; 
		
		return Yii::app()->request->baseUrl . $imagesPath . $src;
	}
	
	static function showPhoto($photos, $id, $entity)
	{
		if (array_key_exists($id, $photos)) {
			$config = new CConfiguration('protected/config/images.php');
			$config = $config->toArray();
			
			$imgSrc = $config[$entity]['uploadPath'] . $config[$entity]['smallImgPrefix'] . '_' . $photos[$id];
			
			if (file_exists($_SERVER['DOCUMENT_ROOT'] . $imgSrc)) {
				return "<img src=\"{$imgSrc}\" class=\"carouselItemImg\">";
			}
		}
		
		return '';
	}
	
	static function showLightBoxPhoto($image, $entity, $title = '&nbsp;')
	{
		$config = new CConfiguration('protected/config/images.php');
		$config = $config->toArray();
		
		$imgMediumSrc = $config[$entity]['uploadPath'] . $config[$entity]['mediumImgPrefix'] . '_' . $image;
		$imgBigSrc    = $config[$entity]['uploadPath'] . $config[$entity]['bigImgPrefix'] . '_' . $image;
		
//		$size = getimagesize($_SERVER['DOCUMENT_ROOT'] . $imgMediumSrc);

		return "<a href=\"{$imgBigSrc}\" title=\"{$title}\"><img src=\"{$imgMediumSrc}\"></a>";
	}

	public function textDelimiter($text, $url, $withOutSpecialchars = false)
    {
    	$countSimbols = 200;
    	
    	$url = '<a href="' . $url . '">[подробнее]</a>';
    	
    	$firstTxtPart  = mb_substr($text, 0, $countSimbols, 'UTF-8');
    	
    	if (mb_strlen($text, 'UTF-8') > $countSimbols) {
	    	$s = '';
			
			for($i = $countSimbols + 1; $i < mb_strlen($text, 'UTF-8'); $i++) {
				$s = $text[$i];
				$s = mb_substr($text, $i - 1, 1, 'UTF-8');
				if ($s != ' ') {
					$firstTxtPart .= $s;
				} else {
					break;
				}
			}
	    	
			if (!$withOutSpecialchars) {
				return htmlspecialchars($firstTxtPart) . '... ' . $url;				
			} else {
				return $firstTxtPart . '... ' . $url;
			}
			

    	} else {
    		if (!$withOutSpecialchars) {
    			return htmlspecialchars($text) . ' ' . $url;
    		} else {
    			return $text . ' ' . $url;
    		}
    	}
    }
}